export default
class ScrollToAnchor {
    constructor() {
      this.findAllElement();
    }
  
    findAllElement() {
      var elements = document.querySelectorAll('[data-anchor]');
  
      elements.forEach(el => {
        el.addEventListener('click', this.moveToTarget);
      });
    }
  
    moveToTarget() {
      try {
        let nameTarget = this.getAttribute('data-anchor');
        const element = document.querySelector(`[data-target='${nameTarget}']`);
        const topPos = element.getBoundingClientRect().top - 20;
  
        window.scroll({
          top: topPos,
          behavior: 'smooth'
        });
  
      } catch(err) {
        console.log(err, 'ScrollToAnchor class');
      }
  
    }
  }